﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Net;
using System.Xml.Linq;
using System.Linq;
using Samples.Radio.Agent;
using Samples.Radio.Data;

namespace Samples.Radio
{
    public class ProgramViewModel : NotifyBase
    {
        private bool isFirst;

        public bool IsFirst
        {
            get { return isFirst; }
            set
            {
                if (IsFirst == value) return;
                isFirst = value;
                RaisePropertyChanged(() => IsFirst);
            }
        }
        

        private string _ProgramName;
        public string ProgramName
        {
            get
            {
                return _ProgramName;
            }
            set
            {
                _ProgramName = value;
                RaisePropertyChanged(()=>ProgramName);
            }
        }

        private string _ProgramImage;
        public string ProgramImage
        {
            get
            {
                return _ProgramImage;
            }
            set
            {
                _ProgramImage = value;
                RaisePropertyChanged(()=>ProgramImage);
            }
        }

        private string _Presenters;
        public string Presenters
        {
            get
            {
                return _Presenters;
            }
            set
            {
                _Presenters = value;
                RaisePropertyChanged(()=>Presenters);
            }
        }

        private string _Description;
        public string Description
        {
            get
            {
                return _Description;
            }
            set
            {
                _Description = value;
                RaisePropertyChanged(()=>Description);
            }
        }

        private string _ProgramSlogan;
        public string ProgramSlogan
        {
            get
            {
                return _ProgramSlogan;
            }
            set
            {
                _ProgramSlogan = value;
                RaisePropertyChanged(()=>ProgramSlogan);
            }
        }

        private string _Schedule;
        public string Schedule
        {
            get
            {
                return _Schedule;
            }
            set
            {
                _Schedule = value;
                RaisePropertyChanged(()=>Schedule);
            }
        }

        private string _Keywords;
        public string Keywords
        {
            get
            {
                return _Keywords;
            }
            set
            {
                _Keywords = value;
                RaisePropertyChanged(()=>Keywords);
            }
        }

        private string _AssociatedPodcastUrl;
        public string AssociatedPodcastUrl
        {
            get
            {
                return _AssociatedPodcastUrl;
            }
            set
            {
                _AssociatedPodcastUrl = value;
                RaisePropertyChanged(()=>AssociatedPodcastUrl);
                GetPodcasts(value);
            }
        }

        private string _StartTime;
        public string StartTime
        {
            get
            {
                return _StartTime;
            }
            set
            {
                _StartTime = value;
                RaisePropertyChanged(()=>StartTime);
                RaisePropertyChanged(()=>Schedule);

                if (value != null)
                {
                    this.StartTimeSpan = TimeSpan.Parse(value);
                }
            }
        }

        private string _EndTime;
        public string EndTime
        {
            get
            {
                return _EndTime;
            }
            set
            {
                _EndTime = value;
                RaisePropertyChanged(()=>EndTime);
                RaisePropertyChanged(()=>Schedule);
                this.EndTimeSpan = TimeSpan.Parse(value);
            }
        }

        private TimeSpan _StartTimeSpan;
        public TimeSpan StartTimeSpan
        {
            get
            {
                return _StartTimeSpan;
            }
            set
            {
                _StartTimeSpan = value;
                RaisePropertyChanged(()=>StartTimeSpan);
            }
        }

        private TimeSpan _EndTimeSpan;
        public TimeSpan EndTimeSpan
        {
            get
            {
                return _EndTimeSpan;
            }
            set
            {
                _EndTimeSpan = value;
                RaisePropertyChanged(()=>EndTimeSpan);
            }
        }
        
        public ObservableCollection<PodcastViewModel> Podcasts { get; set; }

        public void RaiseAllPropertiesChanged()
        {
            RaisePropertyChanged(()=>ProgramName);
            RaisePropertyChanged(()=>ProgramImage);
            RaisePropertyChanged(()=>Presenters);
            RaisePropertyChanged(()=>Description);
            RaisePropertyChanged(()=>Keywords);
            RaisePropertyChanged(()=>StartTime);
            RaisePropertyChanged(()=>EndTime);
            RaisePropertyChanged(()=>Schedule);
            RaisePropertyChanged(()=>ProgramSlogan);
            RaisePropertyChanged(()=>Podcasts);
        }

        public ProgramViewModel()
        {
            this.Podcasts = new ObservableCollection<PodcastViewModel>();
            
        }

        public void GetPodcasts(string url)
        {
            if (!String.IsNullOrEmpty(url))
            {
                GetPodcasts(url, success =>
                {
                    this.Podcasts.Clear();
                    foreach (var item in success)
                    {
                        this.Podcasts.Add(item);

                    }
                });
            }
        }


        public event EventHandler<ParameterEventArgs<PodcastViewModel[]>> LoadPodcastsCompleted;

        /// <summary>
        /// Load all the podcasts from xml
        /// </summary>
        /// <param name="xmlUrl"></param>
        private void GetPodcasts(string xmlUrl, Action<ObservableCollection<PodcastViewModel>> success)
        {
            WebClient client = new WebClient();
            var podcasts = new ObservableCollection<PodcastViewModel>();

            client.OpenReadCompleted += (sender, e) =>
            {
                if (e.Error != null)
                    return;

                Stream str = e.Result;
                XDocument podcastDataXml = XDocument.Load(str);

                var podcastData = from query in podcastDataXml.Descendants("item")
                                  select new PodcastViewModel
                                  {
                                      Title = (string)query.Element("title"),
                                      //Date = DateTime.Parse((string)query.Element("pubDate")),
                                      Description = (string)query.Element("description"),
                                      Keywords = (string)query.Element("keywords"),
                                      PodcastUrl = (string)query.Element("enclosure").Attribute("url")
                                  };

                // close 
                str.Close();

                //Add the podcast to the list
                if (podcastData != null && podcastData.Count() > 0)
                {
                    podcasts = new ObservableCollection<PodcastViewModel>(podcastData);
                    LoadPodcastsCompleted.SafeRaise(this, podcasts.ToArray());
                }

                success(podcasts);
            };
            client.OpenReadAsync(new Uri(xmlUrl, UriKind.Absolute));

        }

     
    }
}